/*
 * @Author: 王灿
 * @Date: 2019-10-27 15:07:02
 * @Description: file content
 */
/* eslint-disable */
// 生成随机字符串
export function random_str(len = 32) {
    const $chars =
        'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    const maxPos = $chars.length
    let str = ''
    for (let i = 0; i < len; i++) {
        str += $chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return str
}

// 深拷贝
function typeOf(obj) {
    const toString = Object.prototype.toString
    const map = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object'
    }
    return map[toString.call(obj)]
}
// deepCopy
export function deepCopy(data) {
    const t = typeOf(data)
    let o

    if (t === 'array') {
        o = []
    } else if (t === 'object') {
        o = {}
    } else {
        return data
    }

    if (t === 'array') {
        for (let i = 0; i < data.length; i++) {
            o.push(deepCopy(data[i]))
        }
    } else if (t === 'object') {
        for (let i in data) {
            o[i] = deepCopy(data[i])
        }
    }
    return o
}

// 金额后面添0（0 => 0.00）
export function parseMoney(s, n) {
    if (!s) return 0
    n = n > 0 && n <= 20 ? n : 2
    s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''
    const l = s
        .split('.')[0]
        .split('')
        .reverse()
    const r = s.split('.')[1]
    let t = ''
    for (let i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 === 0 && i + 1 !== l.length ? ',' : '')
    }
    return (
        t
        .split('')
        .reverse()
        .join('') +
        '.' +
        r
    )
}

export function parseData(time, cFormat) {
    if (arguments.length === 0) {
        return null
    }
    const format = cFormat || '{y}-{m}-{d}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if (('' + time).length === 10) time = parseInt(time) * 1000
        if (time && time.replace) {
            time = time.replace(/-/g, '/')
        }
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        if (key === 'a')
            return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}

// 返回字符串后多少位 默认返回后10位
export function substringLast(str, len) {
    const strLen = len || 10
    if (str) {
        const num = str.length
        if (num - strLen <= 0) {
            return str
        } else {
            return str.substring(num - strLen)
        }
    } else {
        return ''
    }
}

// 缓存计算结果，第二次调用的时候直接读取缓存中的内容
export function cached(fn) {
    const cache = Object.create(null)
    return function cachedFn(str) {
        if (!cache[str]) {
            cache[str] = fn(str)
        }
        return cache[str]
    }
}

export function isOBJByType(o, type) {
    return (
        Object.prototype.toString.call(o) === '[object ' + (type || 'Object') + ']'
    )
}

// localStorage
export function storageSetItem(key, value) {
    localStorage.setItem(key, JSON.stringify(value))
}

// localStorage
export function storageGetItem(key) {
    return JSON.parse(localStorage.getItem(key)) ? JSON.parse(localStorage.getItem(key)) : false
}

// localStorage
export function storageRemoveItem(key) {
    if (storageGetItem(key)) {
        localStorage.removeItem(key)
    } else {
        return false
    }
}